﻿using System;
using UnityEngine;

namespace RootMotion.Demos
{
	// Token: 0x020000E2 RID: 226
	[RequireComponent(typeof(Animator))]
	public class SimpleLocomotion : MonoBehaviour
	{
		// Token: 0x1700009A RID: 154
		// (get) Token: 0x06000686 RID: 1670 RVA: 0x0000631F File Offset: 0x0000451F
		// (set) Token: 0x06000687 RID: 1671 RVA: 0x00006327 File Offset: 0x00004527
		public bool isGrounded { get; set; }

		// Token: 0x06000688 RID: 1672 RVA: 0x00006330 File Offset: 0x00004530
		public void Start()
		{
			this.animator = base.GetComponent<Animator>();
			this.characterController = base.GetComponent<CharacterController>();
			this.cameraController.enabled = false;
		}

		// Token: 0x06000689 RID: 1673 RVA: 0x0003B010 File Offset: 0x00039210
		public void Update()
		{
			this.isGrounded = (base.transform.position.y < 0.1f);
			this.Rotate();
			this.Move();
		}

		// Token: 0x0600068A RID: 1674 RVA: 0x00006356 File Offset: 0x00004556
		public void LateUpdate()
		{
			this.cameraController.UpdateInput();
			this.cameraController.UpdateTransform();
		}

		// Token: 0x0600068B RID: 1675 RVA: 0x0003B04C File Offset: 0x0003924C
		public void Rotate()
		{
			if (!this.isGrounded)
			{
				return;
			}
			Vector3 inputVector = this.GetInputVector();
			if (inputVector == Vector3.zero)
			{
				return;
			}
			Vector3 vector = base.transform.forward;
			SimpleLocomotion.RotationMode rotationMode = this.rotationMode;
			if (rotationMode != SimpleLocomotion.RotationMode.Smooth)
			{
				if (rotationMode == SimpleLocomotion.RotationMode.Linear)
				{
					Vector3 inputVectorRaw = this.GetInputVectorRaw();
					if (inputVectorRaw != Vector3.zero)
					{
						this.linearTargetDirection = this.cameraController.transform.rotation * inputVectorRaw;
					}
					vector = Vector3.RotateTowards(vector, this.linearTargetDirection, Time.deltaTime * (1f / this.turnTime), 1f);
					vector.y = 0f;
					base.transform.rotation = Quaternion.LookRotation(vector);
				}
			}
			else
			{
				Vector3 vector2 = this.cameraController.transform.rotation * inputVector;
				float current = Mathf.Atan2(vector.x, vector.z) * 57.29578f;
				float target = Mathf.Atan2(vector2.x, vector2.z) * 57.29578f;
				float angle = Mathf.SmoothDampAngle(current, target, ref this.angleVel, this.turnTime);
				base.transform.rotation = Quaternion.AngleAxis(angle, Vector3.up);
			}
		}

		// Token: 0x0600068C RID: 1676 RVA: 0x0003B1A0 File Offset: 0x000393A0
		public void Move()
		{
			float target = (!this.walkByDefault) ? ((!Input.GetKey(KeyCode.LeftShift)) ? 1f : 0.5f) : ((!Input.GetKey(KeyCode.LeftShift)) ? 0.5f : 1f);
			this.speed = Mathf.SmoothDamp(this.speed, target, ref this.speedVel, this.accelerationTime);
			float num = this.GetInputVector().magnitude * this.speed;
			this.animator.SetFloat("Speed", num);
			bool flag = !this.animator.hasRootMotion && this.isGrounded;
			if (flag)
			{
				Vector3 a = base.transform.forward * num * this.moveSpeed;
				if (this.characterController != null)
				{
					this.characterController.SimpleMove(a);
				}
				else
				{
					base.transform.position += a * Time.deltaTime;
				}
			}
		}

		// Token: 0x0600068D RID: 1677 RVA: 0x0003B2C4 File Offset: 0x000394C4
		public Vector3 GetInputVector()
		{
			Vector3 result = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
			result.z += Mathf.Abs(result.x) * 0.05f;
			result.x -= Mathf.Abs(result.z) * 0.05f;
			return result;
		}

		// Token: 0x0600068E RID: 1678 RVA: 0x00005573 File Offset: 0x00003773
		public Vector3 GetInputVectorRaw()
		{
			return new Vector3(Input.GetAxisRaw("Horizontal"), 0f, Input.GetAxisRaw("Vertical"));
		}

		// Token: 0x040005FA RID: 1530
		[SerializeField]
		[Tooltip("The component that updates the camera.")]
		public CameraController cameraController;

		// Token: 0x040005FB RID: 1531
		[SerializeField]
		[Tooltip("Acceleration of movement.")]
		public float accelerationTime = 0.2f;

		// Token: 0x040005FC RID: 1532
		[SerializeField]
		[Tooltip("Turning speed.")]
		public float turnTime = 0.2f;

		// Token: 0x040005FD RID: 1533
		[SerializeField]
		[Tooltip("If true, will run on left shift, if not will walk on left shift.")]
		public bool walkByDefault = true;

		// Token: 0x040005FE RID: 1534
		[SerializeField]
		[Tooltip("Smooth or linear rotation.")]
		public SimpleLocomotion.RotationMode rotationMode;

		// Token: 0x040005FF RID: 1535
		[SerializeField]
		[Tooltip("Procedural motion speed (if not using root motion).")]
		public float moveSpeed = 3f;

		// Token: 0x04000600 RID: 1536
		public Animator animator;

		// Token: 0x04000601 RID: 1537
		public float speed;

		// Token: 0x04000602 RID: 1538
		public float angleVel;

		// Token: 0x04000603 RID: 1539
		public float speedVel;

		// Token: 0x04000604 RID: 1540
		public Vector3 linearTargetDirection;

		// Token: 0x04000605 RID: 1541
		public CharacterController characterController;

		// Token: 0x020000E3 RID: 227
		[Serializable]
		public enum RotationMode
		{
			// Token: 0x04000608 RID: 1544
			Smooth,
			// Token: 0x04000609 RID: 1545
			Linear
		}
	}
}
